/*
 * Copyright FUOC.  All rights reserved.

 */
package managedbean;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import jpa.CustomerJPA;
import ejb.CustomerFacadeRemote;

/**
 * Managed Bean ModifyClientMBean
 */
@ManagedBean(name = "modifyClient")
@SessionScoped
public class ModifyClientMBean implements Serializable{
	
	
	private static final long serialVersionUID = 1L;

	@EJB
	private CustomerFacadeRemote customerRemote;

	public CustomerJPA dataCustomer = new CustomerJPA();
	
	public ModifyClientMBean(){
		dataCustomer = (CustomerJPA) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
	}
	
	public CustomerJPA getDataCustomer()
	{
		return dataCustomer;
	}	
	public void setDataCustomer(CustomerJPA customer)
	{
		this.dataCustomer=customer;
	}

	public String clientDetail() throws Exception {

		return "clientDetailView.xhtml";
	}

	/**
	 * Methods to get the params from form.
	 */
 	public String getCustomerNif(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("nifCustomer");
 	}
	public String getCustomerEmail(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
		return params.get("emailCustomer");
 	}
	
	public String getCustomerName(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("nameCustomer");
 	}
 	
 	public String getCustomerSurname(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("surnameCustomer");
 	}
 	
 	public java.sql.Date getCustomerBirthdate(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		try {

	 		java.sql.Date data;
			data = new java.sql.Date(sdf.parse(params.get("birthdateCustomer")).getTime());
			return data;
		} catch (ParseException e) {
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid e-mail or password.", dataCustomer.getEmail());
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
 		return null;
 	}
	
	/**
	 * Method to prepare data to modify
	 */
	public String modifyClientMBean() throws Exception 
	{
		FacesContext fc = FacesContext.getCurrentInstance();
		dataCustomer.setNif(getCustomerNif(fc));
		dataCustomer.setEmail(getCustomerEmail(fc));
		dataCustomer.setName(getCustomerName(fc));
		dataCustomer.setSurname(getCustomerSurname(fc));
		dataCustomer.setBirthdate(getCustomerBirthdate(fc));
		if(dataCustomer.getNif().isEmpty()||dataCustomer.getEmail().isEmpty()||dataCustomer.getName().isEmpty()||dataCustomer.getSurname().isEmpty()||dataCustomer.getBirthdate().toString().isEmpty()){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "You have to fill all the gaps.", null);
			FacesContext.getCurrentInstance().addMessage(null, msg);
			return "modifyClientView";
		}
		modifyData();
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("loginController", dataCustomer);
		return "clientDetailView";
	}
	
	/**
	 * Method to modify data
	 */
	public void modifyData() throws Exception
	{	
	    Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		customerRemote = (CustomerFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/CustomerFacadeBean!ejb.CustomerFacadeRemote");
		dataCustomer = customerRemote.modifyData(dataCustomer);
	}


}
